<?php
/**
 * 会员卡
 * User: ws
 * Date: 2018/8/30
 * Time: 11:45
 */

namespace app\admin\controller;
use app\admin\model\MembershipCard as MeCardModel;
use app\admin\model\UserCardLog as UlogModel;
class Mcard  extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';

            if(is_numeric(input('type')) === true){
                $where['type'] = input('type');
            }
            if(is_numeric(input('state')) === true){
                $where['state'] = input('state');
            }
            $where['name|des|exempted_quota'] = array('like','%'.trim($key).'%');

            $list = MeCardModel::where($where)->order('sort desc ,card_id desc')->paginate(input('limit'))->toArray();
            $url = config('aliyun_oss.url');
            foreach ($list['data'] as $key=>$itme){
                $list['data'][$key]['img'] = str_replace("/card",$url."card",$itme['img']);
                $list['data'][$key]['imgs'] = str_replace("/card",$url."card",$itme['imgs']);
            }
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function add(){
        if(request()->isPost()){
            $data = $this->request->post();
            if($data['type']!=$data['expiry']){
                return json(array('code'=>0,'msg'=>'添加失败，类型和有效期不搭'));
            }
            if(empty($data['img'])){
                return json(array('code'=>0,'msg'=>'添加失败，请上传图片'));
            }
            $data['des'] = base64_decode($data['des']);
            if (!MeCardModel::create($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }
        return view('add');
    }




    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();
            if($data['type']!=$data['expiry']){
                return json(array('code'=>0,'msg'=>'添加失败，类型和有效期不搭'));
            }
            if(empty($data['img']))
                unset($data['img']);

            if(empty($data['imgs']))
                unset($data['imgs']);
            $data['des'] = base64_decode($data['des']);

            if (!MeCardModel::update($data)) {
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
            return json(array('code'=>200,'msg'=>'修改成功'));
        }

        $list = MeCardModel::where('card_id', input('id'))->find()->toArray();
        $url = config('aliyun_oss.url');
        $list['img'] = str_replace("/card",$url."card",$list['img']);
        $list['imgs'] = str_replace("/card",$url."card",$list['imgs']);
        return view('edit',['list'=>$list]);
    }




    function del(){
        $id = input('param.id');
        $data = UlogModel::where('card_id',$id)->find();
        if($data){
            return json(array('code'=>0,'msg'=>'删除失败，已销售不能被删除'));
        }
        if (!MeCardModel::destroy(['card_id' => $id])) {
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
        return json(array('code'=>200,'msg'=>'删除成功'));
    }
}